package com.guslegend.Class01_ioc.test;

import com.guslegend.Class01_ioc.HappyPlay;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author: guslegend
 * @description: 获取Bean
 * @create: 2025-04-27 09:37
 **/
public class GetBean {

    @Test
    public void textGetBean(){
        ApplicationContext iocContainer =
                new ClassPathXmlApplicationContext("bean1.xml");

        //方式1: 根据id获取
        //没有指定类型,返回为Object,需要类型转化!
        HappyPlay happyPlay= (HappyPlay)iocContainer.getBean("happyPlay");
        System.out.print("方法1,根据id获得bean: ");
        happyPlay.play();

        //方式2: 根据类型获取
        //根据类型获取,但是要求,同类型(当前类,或者之类,或者接口的实现类)只能有一个对象交给IoC容器管理
        HappyPlay happyPlay1=iocContainer.getBean(HappyPlay.class);
        System.out.print("方法2,根据类获得bean: ");
        happyPlay.play();

        //方式3: 根据id和类型获取
        HappyPlay happyPlay2=iocContainer.getBean("happyPlay", HappyPlay.class);
        System.out.print("方法3,根据id和类获得bean: ");
        happyPlay.play();
    }
}
